package main

import (
	"fmt"
	"sync"
	"time"
)

/*
 goroutine 结合 channel 管道
*/

// 写数据
func fn1(ch chan int) {
	for i := 1; i <= 10; i++ {
		ch <- i
		fmt.Println("写入管道数据成功-", i)
		time.Sleep(time.Millisecond * 500)
	}
	close(ch)
	wg.Done()
}

// 读数据
func fn2(ch chan int) {
	for v := range ch {
		fmt.Println("读取管道数据成功-", v)
		time.Sleep(time.Millisecond * 600)
	}
	wg.Done()
}

var wg sync.WaitGroup

func main() {
	var ch = make(chan int, 10)

	wg.Add(2)
	go fn1(ch)

	go fn2(ch)
	wg.Wait()
	fmt.Println("done")
}
